<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '格知汇GACC',
    navigationStyle: 'custom',
  },
}
</route>
<template>
  <view class="entire">
    <image
      style="z-index: -10; position: fixed; top: 0; width: 100vw; height: 100vh"
      :src="listBg"
      mode="aspectFill"
    ></image>
    <view class="div1">
      <Backbar title="我的成就" />
      <!-- 一级选择器 -->
      <wht-tabs
        v-model="pageData.tab1"
        :list="pageData.tabList1"
        @change="handleTab1"
        bgColor="#e5effd"
        color="#898889"
        active-color="#254872"
        line-color="#254872"
        :font-size="28"
        :isRound="true"
        :min-width="750"
        :padding="0"
        :lineLength="0.2"
      />
    </view>
    <scroll-view
      ref="scrollContainer"
      scroll-y="true"
      class="div2"
      enable-flex="true"
      :style="{ height: pageData.div2Height + 'px' }"
    >
      <!-- 空显示 -->
      <view
        v-if="pageData.tab1 === 0 && pageData.certList == null"
        style="
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          height: 100%;
        "
      >
        <image src="/static/images/icons/imgNone.png" style="width: 309rpx; height: 259rpx" />
      </view>
      <view @click="navToCert(item)" v-for="(item, index) in pageData.certList" :key="index">
        <CertificationItem2 :cert="item" />
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import CertificationItem2 from '@/components/CertificationItem/CertificationItem2.vue'
import { getAchievements } from '@/service/achievement/achievement'
import pinyin from 'js-pinyin'
import { useUserStore } from '@/store'
import { listBg } from '@/static/imgMap'

const pageData = reactive({
  tab1: 0,
  tabList1: ['我的证书'],
  div2Height: 0,
  certList: [],
})

const navToCert = (item) => {
  uni.navigateTo({
    url: '/pages-sub/myCertificate/certificate?cert=' + encodeURIComponent(JSON.stringify(item)),
  })
}

const handleTab1 = (index) => {
  console.log(index)
}

onMounted(async () => {
  try {
    const response = await getAchievements()
    const userName = useUserStore().userInfo.realName
    const userEngName = useUserStore().userInfo.englishName
    pageData.certList = response.data.map((achievement) => ({
      engName: userEngName,
      name: userName,
      achievementTitle: achievement.achievementTitle,
      code: achievement.number,
      frontImg: achievement.frontImg,
      time: achievement.createTime.split('T')[0],
    }))
  } catch (error) {
    console.error('Error fetching achievements:', error)
  }

  // Self-adaptive scroll list length
  uni.getSystemInfo({
    success(res) {
      const screenHeight = res.windowHeight
      console.log(screenHeight)
      uni
        .createSelectorQuery()
        .select('.div1')
        .boundingClientRect((data) => {
          pageData.div2Height = screenHeight - data.height
        })
        .exec()
    },
  })
})
</script>

<style scoped lang="scss">
.entire {
  width: 100%;
  margin-bottom: 30rpx;
  .div1 {
    background-color: rgb(37, 73, 115);

    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgb(37, 73, 115);
      width: 100%;
      height: 120rpx;
      color: #fff;
      font-size: large;
    }
  }

  .div2 {
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow-y: scroll;
  }
}
</style>
